package com.example.demo9.Filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

        // 允许所有源访问
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        // 其他CORS设置，根据需要配置
         httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
         httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type");
         filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
